#!/bin/bash

trap 'kill $(jobs -p)' EXIT
trap 'kill $(jobs -p)' SIGINT


set -ex

SERVER_PORT=2000
MIN_CLIENT_PORT=4444
COUNT=$1

MAX_NUMBER=`expr $COUNT - 1`

PIDS=""

TESTS=10


for j in [1..$TESTS] do

  echo "TEST nr $j"
  # odpalam grid_server
  ./grid_server localhost:$SERVER_PORT < queries/q$j.ascii
  echo "grid_server pid: $!"
  for i in `seq 0 $MAX_NUMBER`; do
    if [ "$i" == "$MAX_NUMBER" ]; then sleep 3; fi;
    if [ "$i" == "1" ]; then sleep 10; fi;
    ./ne_app_main localhost:$SERVER_PORT $($MIN_CLIENT_PORT + $i) $j& 
    PIDS="$PIDS $!"
  done

  echo $PIDS
  wait

done
